package com.team7.order.controller;



import com.team7.order.dao.OrderDao;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.team7.order.dao.PayLogDao;
import com.team7.order.entity.Order;
import com.team7.order.entity.PayLog;
import com.team7.order.service.OrderService;
import com.team7.order.utils.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author 张煜杰
 * @since 2021-10-22
 */
@RestController
@RequestMapping("/order/order")
@Api(tags = "订单功能模块")
public class OrderController {
    @Resource
    private OrderService orderService;
    @Resource
    private OrderDao orderDao;
    @Resource
    private PayLogDao payLogDao;

    @ApiOperation(value = "查看所有的信息")
    @PostMapping("/getAllOrder/{page}/{limit}")
    public CommonResult getAllOrder(@PathVariable("page") Integer page, @PathVariable("limit") Integer limit, @RequestBody Order order) {
        CommonResult commonResult = orderService.getAllOrder(page, limit, order);
        return commonResult;
    }
    @ApiOperation(value = "根据id进行两表联查")
    @PostMapping("queryOrderAndPayLog/{id}")
    public CommonResult queryOrderAndPayLog(@PathVariable("id") @RequestBody String id){
        CommonResult commonResult = orderService.queryOrderAndPayLog(id);
        return commonResult;
    }
    @ApiOperation(value = "根据课程id查询所有的订单信息")
    @GetMapping("/queryOrderByPay")
    public CommonResult queryOrderByPay(){
        CommonResult commonResult = orderService.queryOrderByPay();
        return commonResult;
    }

    @ApiOperation(value = "根据id进行两表联查")
    @DeleteMapping("/deleteOrder/{id}")
    public CommonResult deleteOrder(@PathVariable("id") String id){
        return orderService.deleteOrder(id);
    }
    //    根据课程id和会员id查询是否购买
    @ApiOperation(value = "根据课程id和会员Id查询支付状态")
    @PostMapping("/getOrderStatus/{id}/{HuiYuanId}")
    public CommonResult getOrderStatus(@PathVariable("id") String id,@PathVariable("HuiYuanId") String HuiYuanId){

        CommonResult status = orderService.getOrderStatusByCourseIdAndHuiYuanId(id, HuiYuanId);
        return status;
    }
    //    根据课程id和会员更改状态
    @ApiOperation(value = "更改支付宝状态")
    @PostMapping("/updateZhiFuStatus/{id}/{HuiYuanId}")
    public CommonResult updateZhiFuStatus(@PathVariable("id") String id,@PathVariable("HuiYuanId") String HuiYuanId){
        CommonResult commonResult = orderService.updateZhiFuStatus(id, HuiYuanId);
        return commonResult;
    }
    //    根据课程id和会员更改状态
    @ApiOperation(value = "更改支付宝状态")
    @GetMapping("/updateStatus/{order_no}")
//    public CommonResult updateStatus(@PathVariable String order_no, HttpServletResponse response) throws IOException {
    public CommonResult updateStatus(@PathVariable String order_no, HttpServletResponse response) throws IOException {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",order_no);
        Order order = orderDao.selectOne(wrapper);
        CommonResult commonResult = orderService.updateStatus(order_no);
//      流水号是否成功
        QueryWrapper<PayLog> queryWrapper = new QueryWrapper<>();
        PayLog payLog = new PayLog();
        payLog.setTradeState("SUCCESS");
        wrapper.eq("order_no",order_no);
        payLogDao.update(payLog,queryWrapper);

        response.sendRedirect("http://192.168.31.201:8081/DetailId/"+order.getCourseId());
        return commonResult;
    }
    //    根据更改状态
    @ApiOperation(value = "更改支付宝状态")
    @GetMapping("/updateZFBStatus/{order_no}")
//    public CommonResult updateStatus(@PathVariable String order_no, HttpServletResponse response) throws IOException {
    public CommonResult updateZFBStatus(@PathVariable String order_no, HttpServletResponse response) throws IOException {
//        更改支付状态
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",order_no);
        Order order = orderDao.selectOne(wrapper);
        CommonResult commonResult = orderService.updateStatus(order_no);

//      流水号是否成功
        QueryWrapper<PayLog> queryWrapper = new QueryWrapper<>();
        PayLog payLog = new PayLog();
        payLog.setTradeState("SUCCESS");
        wrapper.eq("order_no",order_no);
        payLogDao.update(payLog,queryWrapper);
        response.sendRedirect("http://192.168.31.201:8081/myOrder");
        return commonResult;
    }

    //    根据当前课程id和会员id查询当前订单信息
    @ApiOperation(value = "查询当前订单的信息")
    @PostMapping("/queryCurrentOrderXinXi/{id}/{HuiYuanId}")
    public CommonResult queryCurrentOrderXinXi(@PathVariable("id") String id,@PathVariable("HuiYuanId") String HuiYuanId){
        CommonResult commonResult = orderService.queryCurrentOrderXinXi(id, HuiYuanId);
        return commonResult;
    }

}

